/**
 * Copyright (c) 2002-2012 旭鸣信息
 * All rights reserved. 
 * File：ArticlePageListDirective.java
 * History: Dec 27, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.base.Constant;
import com.xteam.ecsystem.service.front.FrontArticlesService;
import com.xteam.ecsystem.utils.DirectiveUtil;
import com.xteam.ecsystem.utils.PageCenterStation;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 站内快讯分页列表 标签
 * @author liuzhi
 */
@Service
public class ArticlePageListDirective implements TemplateDirectiveModel {

	@Autowired
	private FrontArticlesService articlesService;

	@SuppressWarnings("unchecked")
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// 文章类型
		String baiGroup = DirectiveUtil.getString(params, Constant.PAR_BAIGROUP.value());
		// 文章标题
		String baiTitle = DirectiveUtil.getString(params, Constant.PAR_BAITITLE.value());
		// 当前页码
		int pageIndex = DirectiveUtil.getInteger(params, Constant.PAR_PAGEINDEX.value());
		// 每页显示的条数
		int pageSize = DirectiveUtil.getInteger(params, Constant.PAR_PAGESIZE.value());
		
		// 获得站内快讯分页列表
		PageCenterStation<?> p = this.articlesService.getArticleList(baiGroup, baiTitle, pageIndex, pageSize);
		
		env.setVariable(Constant.VAR_ARTICLELISTPM.value(),DEFAULT_WRAPPER.wrap(p));
		
		body.render(env.getOut());
	}
}
